package com.mayikt.weixin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mayikt.base.BaseApiService;
import com.mayikt.base.BaseResponse;
import com.mayikt.constants.Constants;
import com.mayikt.utils.RedisUtils;
import com.mayikt.weixin.service.VerificationCodeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Classname VerificationCodeServiceImpl
 * @Description SpringBoot SpringCloud 构建微服务电商平台。
 * @Created by CHEN JIN LONG
 * @Date 2019/1/18 22:40
 * @Version 1.0
 */
@RestController
public class VerificationCodeServiceImpl extends BaseApiService<JSONObject> implements VerificationCodeService {

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public BaseResponse<JSONObject> verificationCode(String phone, String weixinCode) {

        if (StringUtils.isEmpty(phone)) {

            return setResultError("手机号码不能为空");
        }
        if (StringUtils.isEmpty(weixinCode)) {

            return setResultError("验证码不能为空");
        }
        String codeKey = Constants.WEIXINCODE_KEY + "_" + phone;
        String redisCode = redisUtils.get(codeKey).toString();
        if (StringUtils.isEmpty(redisCode)) {
            return setResultError("验证码不存在");
        }
        if (!redisCode.equals(weixinCode)) {
            return setResultError("验证码不正确");
        }
        //移除redis中验证码
        redisUtils.del(codeKey);
        return setResultSuccess("验证成功");

    }
}
